package cn.edu.imau.zy.student.controller;

import cn.edu.imau.zy.student.service.IStStudentFamilyService;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.common.core.entity.student.StStudentFamily;

import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.controller.BaseController;

/**
 * 家庭成员情况 提供者
 *
 * @author wyz
 * @date 2020-10-26
 */
@RestController
@RequestMapping("family")
public class StStudentFamilyController extends BaseController {

    @Autowired
    private IStStudentFamilyService stStudentFamilyService;

    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{id}")
    public StStudentFamily get(@PathVariable("id") String id) {
        return stStudentFamilyService.selectStStudentFamilyById(id);

    }

    /**
     * 查询家庭成员情况列表
     */
    @GetMapping("list")
    public R list(StStudentFamily stStudentFamily) {
        return R.ok().put("list", stStudentFamilyService.selectStStudentFamilyList(stStudentFamily));
    }


    /**
     * 新增保存家庭成员情况
     */
    @PostMapping("save")
    public R addSave(@RequestBody StStudentFamily stStudentFamily, @LoginUser SysUser user) {
        stStudentFamily.setCreateBy(user.getUserId());
        stStudentFamily.setId(IdUtil.getSnowflake(2L, 2L).nextIdStr());
        if (stStudentFamilyService.insertStStudentFamily(stStudentFamily) > 0) {
            return R.ok().put("id", stStudentFamily.getId());
        }
        return R.error();
    }

    /**
     * 修改保存家庭成员情况
     */
    @PostMapping("update")
    public R editSave(@RequestBody StStudentFamily stStudentFamily, @LoginUser SysUser user) {
        stStudentFamily.setUpdateBy(user.getUserId().toString());
        return toAjax(stStudentFamilyService.updateStStudentFamily(stStudentFamily));
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(stStudentFamilyService.deleteStStudentFamilyByIds(ids));
    }

}
